package com.bowen.chapter06.refactor;

import com.bowen.chapter06.refactor.type.EmployeeType;
import com.bowen.except.EmployeeTypeInValidException;

public class Employee {

    /**
     * 员工姓名
     */
    protected String name;

    /**
     * 员工类型
     */
    protected String type;


    public Employee(String name, String type) {
        this.name = name;
        this.type = type;
    }

    /**
     * 重构第十一章 重构API 以工厂函数取代构造函数
     *
     * @param name
     * @param type
     * @return
     * @throws EmployeeTypeInValidException
     */
    public static Employee createEmployee(String name, String type) throws EmployeeTypeInValidException {
        if ("engineer".equals(type)) {
            return new Engineer(name, type);
        } else if ("salesman".equals(type)) {
            return new Salesman(name, type);
        }
        throw new EmployeeTypeInValidException();
    }

    // ----------------------------------------------------------------------------------------------------
    // -----------------------------------------员工类型可扩展-----------------------------------------------
    // ----------------------------------------------------------------------------------------------------
    protected EmployeeType eType;

    public Employee(String name, EmployeeType eType) {
        this.name = name;
        this.eType = eType;
        this.type = this.eType.getType();// 这个复制只是因为DEMO中他们共存才复制，实际项目中使用一个即可
    }

    public static Employee createEmployee(String name, EmployeeType eType) throws EmployeeTypeInValidException {
        if ("engineer".equals(eType.getType())) {
            return new Engineer(name, eType);
        } else if ("salesman".equals(eType.getType())) {
            return new Salesman(name, eType);
        }
        throw new EmployeeTypeInValidException();
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public EmployeeType geteType() {
        return eType;
    }

    public void seteType(EmployeeType eType) {
        this.eType = eType;
    }
}
